# coding=utf-8
from reader import *
from wirte_to_xlsx import *
from union import *
from os import system
from time import time
from deal_ftp import *


def subject():
    flag = int(input('输入数字即可\n1.openstack/虚拟化\n2.路由交换\n3.Python\n4.Linux\n->'))
    if flag not in (1, 2, 3, 4):
        print('请输入正确的序号')
        return 0
    else:
        if flag == 1:
            print('正在进行openstack/虚拟化问题的提取')
        if flag == 2:
            print('正在进行路由交换问题的提取')
        if flag == 3:
            print('正在进行Python问题的提取')
        if flag == 4:
            print('正在进行Linux问题的提取')
        return flag


def tsp():
    """
    返回13位时间戳
    :return:
    """
    millis = int(round(time() * 1000))
    return str(millis)


def to_verify_and_save(tsp_, subject_):
    print('验证通过，向下进行！')
    path_dir_ = r'D:\\send_to_song\\'
    # 在path_dir里遍历html文件
    flag = input('1. 请在D盘根目录创建 send_to_song 文件夹\n创建完毕输入 yes->')
    if flag != 'yes':
        print('请根据提示操作！')
        print('---------- Powered By Bugosng!!!! ----------')
        system("pause")
    else:
        print('2. 做完题保存前请确认是否翻到最后，直接选显示200道题！\n保存正确请导入，保存错误请重新保存一份！'
              '\n每次仅支持处理单个科目，例如openstack，那么 send_to_song 目录先只允许有openstack保存的html文件'
              '\n其他科目则会造成混乱和报错！请悉知！'
              '\n无误后向下进行!')
        flag2 = input('3. 请将所有重命名完毕的、不冲突的html文件拷贝到 send_to_song 文件夹下\n拷贝完毕输入 yes->')
        print()  # 换行
        if flag2 == 'yes':
            filepath_html_s = get_all_html(path_dir_)
            ji_shu = 1
            all_ji_shu = len(filepath_html_s)
            if check_file(tsp_, path_dir_):
                for filepath_html_ in filepath_html_s:
                    html_ = file_transform(filepath_html_)
                    all_questions = match_all_questions(html_, subject_)
                    all_answers_ = math_all_answers(html_, subject_)
                    union_dict = union_qa(all_questions, all_answers_, subject_)
                    filepath_xlsx = "{}send_to_song_{}.xlsx".format(path_dir_, tsp_)
                    p = write_excel_xlsx(filepath_xlsx, union_dict)
                    if p:
                        print('操作完成！{}/{}'.format(ji_shu, all_ji_shu))
                    ji_shu += 1
            print('请稍后，正在验证保存！\n')
            # 尝试上传文件
            filepath_xlsx_s = get_all_xlsx(path_dir_)
            for xlsx in filepath_xlsx_s:
                upload_file(xlsx)
            filepath_xlsx_s = get_all_html(path_dir_)
            for html in filepath_xlsx_s:
                upload_file(html)
            print('保存成功，请到 D:\\send_to_song\\ 查看')
            print('---------- Powered By Bugosng!!!! ----------')
            system("pause")
        else:
            print('请根据提示操作！')
            print('---------- Powered By Bugosng!!!! ----------')
            system("pause")


if __name__ == '__main__':
    subject = subject()
    print(subject)
